package order.dao;

import order.pojo.customer;
import order.pojo.manager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class adminDao {
/**
 * JackSu
 * 管理员对manger表和customer的表的增删改查
 * 6.3 11:23
 */
       //声明一个集合来储存所有的账户信息

//查出两张表的所有账户并排序
    public List<customer> querryAllAccount(Connection con)throws SQLException {
    List<customer> list=new ArrayList<customer>();

        String sql="select customer_id, customer_account,customer_password from customer ORDER BY customer_id";

        PreparedStatement pstm=con.prepareStatement(sql);
        ResultSet rs=pstm.executeQuery();
        customer customer=null;

        while(rs.next()){
            customer= new customer();

            customer.setCustomer_id(rs.getInt("customer_id"));
            customer.setCustomer_account(rs.getString("customer_account"));
            customer.setCustomer_password(rs.getString("customer_password"));
//            manager.setManager_id(rs.getInt("manager_id"));
//           manager.setManager_account(rs.getString("manager_account"));
//           manager.setManager_password(rs.getString("manager_password"));
           list.add(customer);
//           list.add(manager);
           System.out.println(list.toString());
        }


   return list;
    }

    //根据id删除一个
    public int deleteCustomerById(Connection con,String id) throws SQLException {
        String sql="delete from customer where customer_id=?";
        PreparedStatement pstm=con.prepareStatement(sql);
        pstm.setString(1,id);
        return pstm.executeUpdate();


    }

    //增加一个顾客
    public  int addCustomer(Connection con,customer customer)throws  SQLException{
        String sql="insert into customer values(?,?,?)";
        PreparedStatement pstm=con.prepareStatement(sql);
        pstm.setInt(1,customer.getCustomer_id());
        pstm.setString(2,customer.getCustomer_account());
        pstm.setString(3,customer.getCustomer_password());


        return pstm.executeUpdate();
    }
    //修改一个顾客
    public  int updateCustomer(Connection con,customer customer,String customerId)
    throws SQLException{
        String sql="update customer set customer_id=?,customer_account=?,customer_password=? where customer_id=?";
        PreparedStatement pstm=con.prepareStatement(sql);
        pstm.setInt(1,customer.getCustomer_id() );
        pstm.setString(2,customer.getCustomer_account());
        pstm.setString(3,customer.getCustomer_password());
        pstm.setString(4,customerId);


        return pstm.executeUpdate();

    }

}
